package com.pubsys.main.client.componentes.barramenu;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.pubsys.main.client.aplicacao.AppHistoryController;
import com.pubsys.main.client.componentes.botao.ButtonClickEvent;
import com.pubsys.main.client.componentes.botao.ButtonWidget;
import com.pubsys.main.client.paginas.consulta.PaginaConsulta;
import com.pubsys.main.client.paginas.insercao.Insercao;
import com.pubsys.main.client.paginas.login.PaginaLogin;
import com.pubsys.main.client.paginas.paginaentrada.PaginaEntrada;
import com.pubsys.main.client.rpc.login.LoginService;
import com.pubsys.main.client.rpc.login.LoginServiceAsync;
import com.pubsys.main.shared.model.Autor;

public class MenuUI extends Composite {

	private static MenuUIUiBinder uiBinder = GWT.create(MenuUIUiBinder.class);
	@UiField ButtonWidget consultaButton;
	@UiField ButtonWidget buttonWidget;
	@UiField ButtonWidget buttonWidget_1;
	@UiField ButtonWidget incluirButton;
//	private LoginInfo loginInfo = null;

	interface MenuUIUiBinder extends UiBinder<Widget, MenuUI> {
	}

	public MenuUI() {
		initWidget(uiBinder.createAndBindUi(this));
		// Check login status using login service.
	    LoginServiceAsync loginService = GWT.create(LoginService.class);
	    loginService.pegaUsuarioLogado(new AsyncCallback<Autor>() {			
			@Override
			public void onSuccess(Autor result) {
				incluirButton.setVisible((result != null && result.getEmail() != null && !result.getEmail().trim().equals("")));
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
			}
		});
	}

	@UiHandler("consultaButton")
	void onMobileButtonWidgetClick(ButtonClickEvent event) {
		AppHistoryController.getInstance().goToActivity(new PaginaConsulta());
	}
	@UiHandler("buttonWidget")
	void onButtonWidgetClick(ButtonClickEvent event) {
		AppHistoryController.getInstance().goToActivity(new PaginaEntrada());
	}
	
	@UiHandler("buttonWidget_1")
	void onButtonWidget_1Click(ButtonClickEvent event) {
		AppHistoryController.getInstance().goToActivity(new PaginaLogin());
	}
	@UiHandler("incluirButton")
	void onIncluirButtonClick(ButtonClickEvent event) {
		AppHistoryController.getInstance().goToActivity(new Insercao());
	}
	
	
}
